<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;


use App\Common\Sql;
use App\Exceptions\ApiException;
use App\Logic\MiGuLogic;

class MiGuLogin extends Base
{

    public function logic()
    {
        //咪咕token
        if (empty($this->param['identityId'])) {
            throw new ApiException('migu_token must be needed',330);
        }

        //手机型号
        if (!isset($this->param['ua'])) {
            $ua = "";
        } else if (strlen($this->param['ua']) > 20) {
            $ua = substr($this->param['ua'], 0, 20);
        } else {
            $ua = $this->param['ua'];
        }

        $migu_id = MiGuLogic::instance()->get_miguid_from_token($this->param['identityId']);

        $login_ip = $_SERVER["REMOTE_ADDR"];

        //该ID是否已经注册，如果是直接登录，如果不是注册
        $row = Sql::select('t_player_info', [['migu_id', $migu_id]]);
        if (count($row) >= 1) {
            $user_info = MiGuLogic::instance()->hasRegistered($row,$login_ip);
        } else {
            $user_info = MiGuLogic::instance()->noHasRegistered($ua,$migu_id,$login_ip);
        }
        //每日登陆任务
        $this->add_task_unclaimed_num((int)$user_info['uid'], 2);
        return $this->returnData($user_info);
    }
}